/**
 * package: models
 * author: zhaiyujin
 * description:
 */
package models

import (
	"database/sql/driver"
	"fmt"
	"time"
)

type MyTime struct {
	time.Time
}

var ExTimeUnmarshalTimeFormat = "2006-01-02 15:04:05"
var ExTimeMarshalTimeFormat = "2006-01-02 15:04:05"

func (m MyTime) UnmarshalTimeFormat() string {
	return ExTimeUnmarshalTimeFormat
}

func (m MyTime) MarshalTimeFormat() string {
	return ExTimeMarshalTimeFormat
}
func (m MyTime)  MarShalJSON()([]byte ,error){
	//var stamp = fmt.Sprintf("%s", time.Time(m.Time).Format(m.MarshalTimeFormat()))
	var stamp = fmt.Sprintf("%s", m.Format(m.MarshalTimeFormat()))
	return []byte(stamp), nil
}
// Value insert timestamp into mysql need this function.
func (m MyTime) Value() (driver.Value, error) {
	var zeroTime time.Time
	if m.Time.UnixNano() == zeroTime.UnixNano() {
		return nil, nil
	}
	return m.Time, nil
}

// Scan valueof time.Time
func (m *MyTime) Scan(v interface{}) error {
	value, ok := v.(time.Time)
	if ok {
		*m = MyTime{Time: value}
		return nil
	}
	return fmt.Errorf("can not convert %v to timestamp", v)
}